/**
 * @(#)CrawlServlet.java, 2010-5-12. 
 * 
 */
package com.rsstuan.web.cron.controller;


import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.rsstuan.crawl.impl.CrawlerService;
import com.rsstuan.service.ChannelService;
import com.rsstuan.storage.data.TuanGouChannelData;
import com.rsstuan.storage.impl.JDOTuanGouChannelStorage;
import com.rsstuan.web.controller.SpringController;


/**
 *
 * @author guosq
 *
 */

public class CrawlController  extends SpringController {
    
	private static final Logger LOG = Logger.getLogger(CrawlController.class.getName());
    
	public ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) {
		
       try {
    	   LOG.info("CrawlServlet begins");
   			CrawlerService service = new CrawlerService();
    	   if (request.getParameter("channelId") != null) {
    		   Long channelId = Long.parseLong(request.getParameter("channelId"));
    		   JDOTuanGouChannelStorage channelStorage = new JDOTuanGouChannelStorage();
    		   TuanGouChannelData channel = channelStorage.getChannel(channelId);
    		   service.doCrawlOne(channel);
    	   }
    	   else {
    		   	service.doCrawl();
    	   }
    	   response.getWriter().println("Crawl finished");
       }
       catch (Exception e) {
    	   LOG.log(Level.WARNING, "crawl failed", e);
       }
       return null;
      
    }
}
